package com.wbao.dianniu.baidumap;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.search.poi.PoiResult;
import com.wbao.dianniu.R;

/**
 * 用于显示poi的overly
 */
public class PoiOverlay extends OverlayManager {

    private PoiResult mPoiResult = null;
    private int preIndex;
    
    /**
     * 构造函数
     * 
     * @param baiduMap
     *            该 PoiOverlay 引用的 BaiduMap 对象
     */
    public PoiOverlay(BaiduMap baiduMap) {
        super(baiduMap);
    }

    /**
     * 设置POI数据
     * 
     * @param poiResult
     *            设置POI数据
     */
    public void setData(PoiResult poiResult) {
        this.mPoiResult = poiResult;
        preIndex = 0;
    }
    
    /**
     * 显示当前点击的marker
     * @param index
     */
    public void showCurrentmarker(int index){
    	 if (mPoiResult == null || mPoiResult.getAllPoi() == null) {
             return;
         }
    	Marker marker = (Marker) mOverlayList.get(index);
    	marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding));
    	preIndex = index;
    }
    
    /**
     * 取消上次点击的marker
     */
    public void dismissMarker(){
    	Marker marker = (Marker) mOverlayList.get(preIndex);
    	marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_coding));
    }

    @Override
    public final List<OverlayOptions> getOverlayOptions() {
        if (mPoiResult == null || mPoiResult.getAllPoi() == null) {
            return null;
        }
        List<OverlayOptions> markerList = new ArrayList<OverlayOptions>();
        for (int i = 0; i < mPoiResult.getAllPoi().size(); i++) {
            if (mPoiResult.getAllPoi().get(i).location == null) {
                continue;
            }
            Bundle bundle = new Bundle();
            bundle.putInt("index", i);
            if(i == 0){ //默认第一个显示蓝色
            	markerList.add(new MarkerOptions()
                .icon( BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding)).extraInfo(bundle)
                .position(mPoiResult.getAllPoi().get(i).location));
            }else{
            	markerList.add(new MarkerOptions()
                .icon( BitmapDescriptorFactory.fromResource(R.drawable.icon_coding)).extraInfo(bundle)
                .position(mPoiResult.getAllPoi().get(i).location));
            }
        }
        return markerList;
    }

    /**
     * 获取该 PoiOverlay 的 poi数据
     * 
     * @return
     */
    public PoiResult getPoiResult() {
        return mPoiResult;
    }

    public void setOverlay(int index,Overlay overlay){
    	mOverlayList.set(index, overlay);
    }
    
    /**
     * 覆写此方法以改变默认点击行为
     * 
     * @param i
     *            被点击的poi在
     *            {@link PoiResult#getAllPoi()} 中的索引
     * @return
     */
    public boolean onPoiClick(int i) {
        if (mPoiResult.getAllPoi() != null && mPoiResult.getAllPoi().get(i) != null) {
        }
        return false;
    }
    
    public boolean onPoiClick(Marker marker){
    	 return false;
    }
    

    @Override
    public final boolean onMarkerClick(Marker marker) {
        if (!mOverlayList.contains(marker)) {
            return false;
        }
        if (marker.getExtraInfo() != null) {
            return onPoiClick(marker.getExtraInfo().getInt("index"));
        }
        return false;
    }

    @Override
    public boolean onPolylineClick(Polyline polyline) {
        // TODO Auto-generated method stub
        return false;
    }
}
